
<template>
    <div>
      <avue-crud v-bind="bindVal"
          v-on="onEvent"
          v-model="form"
          :page.sync="page">
          <template slot="menuLeft" slot-scope="scope">
             <el-button type="primary" size="small" @click.native="addInfo" icon="el-icon-plus">新增告警</el-button>
          </template>
         <template slot="menu" slot-scope="scope">
            <el-button type="text" size="small" @click.native="rowEdit(scope.row)" icon="el-icon-edit">查看</el-button>
            <!-- <el-button type="text" size="small" @click.native="rowView(scope.row)" icon="el-icon-view">告警日志</el-button> -->
            <!-- <template v-if="scope.row.activated === true">
              <el-popconfirm style="padding-right:6px"
                  title="确认停用此告警？"
                  @onConfirm="setSetting(scope.row)"
                  >
                  <el-button style="padding-left:6px; color: green" 
                            type="text" size="small" icon="el-icon-check"
                            slot="reference">启动</el-button>
              </el-popconfirm>
            </template>
             <template v-else>
              <el-popconfirm style="padding-right:6px"
                  title="确认启动此告警？"
                  @onConfirm="setSetting(scope.row)"
                  >
                  <el-button style="padding-left:6px; color:red" 
                            type="text" size="small" icon="el-icon-close"
                            slot="reference">停用</el-button>
              </el-popconfirm>
             </template> -->
            <el-button type="text" size="small" @click.native="rowDel(scope.row,scope.index)" icon="el-icon-delete">删除</el-button>
          </template>
          <template slot="activated" slot-scope="scope">
            <el-switch
                :active-value="true"
                :inactive-value="false"
                active-text="启用"
                inactive-text="停用"
                active-color="#13ce66"
                inactive-color="" 
                v-model="scope.row.activated"
                :disabled="disabled" 
                @change="setSetting(scope.row)">
            </el-switch>
          </template>
      </avue-crud>
      <alarm-dialog ref="addinfo" :title="title" @getlist="addList"></alarm-dialog>
    </div>
</template>
<script>

import AlarmDialog from "./alarm-dialog"
// import {release, unRelease } from '@/api/device/instance/alarm-list'

export default window.$crudCommon({
  components: { AlarmDialog },
  data () {
    return {
      title:'新增告警',
      form: {},
      params: {
       productId: this.$route.query.productId,
     },
     disabled: '',
    }
  },
  props:['list'],
  computed:{
    // disabled: function(){
    //   return this.list.released === false ? false : true
    // }
  },
  created(){
    this.getStatus()
  },
  methods: {
    getStatus(){
      if(this.list.released === true){
        this.disabled = false
      }else{
        this.disabled = true
      }
      return this.disabled
    },
    addList(){
      this.getList()
    },
    //新增告警
    addInfo(){  
      this.title = '新增告警'
      this.$refs.addinfo.dialogVisible = true
      this.$refs.addinfo.ways = 'add'
    },
    //启动
    setSetting(row) {
      if(row.activated === false){ //停用产品告警
          unRelease(row.warningId).then(res => {
            if(res.data.code === 0){
              this.$message.success('停用产品告警成功')
            }
          }).catch(err =>{
            row.activated = 1
          })
      }else{
        release(row.warningId).then(res => {
            if(res.data.code === 0){
              this.$message.success('启用产品告警成功')
            }
          }).catch(err =>{
            row.activated = 0
          })
      }
    },
    //列表更新后的操作
    listAfter() {},
    //查看
    rowEdit(row){
      this.title = '编辑告警'
      this.$refs.addinfo.dialogVisible = true
      this.$refs.addinfo.form = row
      this.$refs.addinfo.getWarning(row.warningId)
      this.$refs.addinfo.ways = 'update'
    },
    //告警日志
    rowView(row){
      this.$emit('updateInfo',row)
    }
  }
}, {
  name:'device/instance/alarm-list',//模块名字
  list: 'list',//列表接口名字
  update: 'updateObj',//更新接口名字
  add: 'addObj',//新增接口名字
  del: 'delObj',//删除接口名字
  rowKey: 'warningId',//主键
  pageNumber: 'current',//页码
  pageSize: 'size',//页数
  res:(data)=>{
    return {
      total:0,
      data:[]
    }
  },//列表的结构
  total: 'total',//总页数
  data: 'records'//列表属性
})
</script>
<style lang="scss" scoped>
</style>


